//index.js
//获取应用实例
const app = getApp()
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
Page({
  data: {
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    banner:[],
    fuwu:[],
    cleaning:[]
  },
  //事件处理函数
  onLoad: function () {
    this.getBanner();
    this.getFuwu();
    this.getCleaning();
    this.dialog = this.selectComponent("#dialog");
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              //从数据库获取用户信息
              that.dialog.queryUsreInfo();
            }
          });
        } else {
          that.showDialog();
        }
      }
    })
  },
  showDialog: function () {
    this.dialog.showDialog();
  },
  confirmEvent: function () {
    this.dialog.hideDialog();
  },
  gotoItem:function(e){
    let that = this
    console.log(e)

      let currenttab = e.currentTarget.dataset.currenttab
    app.globalData.currenttab = currenttab
    wx.switchTab({
      url: '/pages/items/items',
      success: function (e) {
        var page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onLoad();
      }
    })
  },
  gotoEat:function(){
    wx.navigateTo({
      url: '/pages/eat/eat',
    })
  },
  gotoEnvironment:function(){
    wx.navigateTo({
      url: '/pages/environment/environment',
    })
  },
  gotoCleaning:function(){
    wx.switchTab({
      url: '/pages/cleaning/cleaning',
    })
  },
  getList() {
    this.selectComponent("#header").getAddress()
  },
  // 获取banner
  getBanner(){
    network.request({
      url: config.host+'api_banner/banner',
      method:'POST',
      success:(res)=>{
        if(res.data.code==99){
          console.log("banner数据")
          console.log(res.data.data)
          this.setData({
            banner:res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    })
  },
  //获取推荐项目
  getFuwu(){
    network.request({
      url: config.host + 'api_package/packageRecommended',
      method: 'POST',
      success: (res) => {
        if (res.data.code == 99) {
          console.log("推荐项目")
          console.log(res.data.data)
          this.setData({
            fuwu: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    })
  },
  //获取保洁师推荐
  getCleaning(){
    network.request({
      url: config.host + 'api_cleaner/cleanerRecommended',
      method: 'POST',
      success: (res) => {
        if (res.data.code == 99) {
          console.log("保洁师推荐")
          console.log(res.data.data)
          this.setData({
            cleaning: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    })
  }
})
